package cn.alcatraz.utils;

import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;

/**
* @Description: Java连接ES工具类
* @Author: Alcatraz
* @Version: 1.0
* @Date:  2022/7/24 20:44
*/

public class ESClientUtil {

    public static TransportClient getClient(){
        TransportClient client = null;
        Settings settings = Settings.builder()
                .put("cluster.name", "elasticsearch").build();
        try {
            client = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return client;
    }


    /**
    * @Title: testAdd
    * @Description: 添加文档
    * @Author: Alcatraz
    * @Version: 1.0
    * @Date:  2022/7/24 20:44
    * @Parameters: []
    * @Return void
    */
    @Test
    public void testAdd() throws Exception{
        // 1.获取ES连接
        TransportClient client = ESClientUtil.getClient();
        // 2.准备要放入到ES中的数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", "1");
        map.put("name", "你老人家吃了吗?");
        map.put("age", "66");
        map.put("sex", "1");
        // 3.使用链式编程，将数据存入到ES中
        IndexRequestBuilder builder = client.prepareIndex("pethome", "user", "1").setSource(map);
        // 4.得到添加文档结果
        System.out.println(builder.get());
        // 5.关闭连接
        client.close();
    }

    /**
    * @Title: testGet
    * @Description: 根据ID查询
    * @Author: Alcatraz
    * @Version: 1.0
    * @Date:  2022/7/24 20:44
    * @Parameters: []
    * @Return void
    */
    @Test
    public void testGet() throws Exception{
        // 1.获取ES连接
        TransportClient client = ESClientUtil.getClient();
        // 2.获取指定文档
        GetResponse response = client.prepareGet("pethome", "user", "1").get();
        System.out.println(response);
        System.out.println(response.getSource());
        // 3.关闭连接
        client.close();
    }

    /**
    * @Title: testUpt
    * @Description: 根据ID修改
    * @Author: Alcatraz
    * @Version: 1.0
    * @Date:  2022/7/24 20:43
    * @Parameters: []
    * @Return void
    */
    @Test
    public void testUpt() throws Exception{
        // 1.获取ES连接
        TransportClient client = ESClientUtil.getClient();
        // 2.准备要修改的数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", "您老人家吃早饭了吗?");
        // 3.使用链式编程，将要修改的数据存入到ES中
        UpdateResponse response = client.prepareUpdate("pethome", "user", "1").setDoc(map).get();
        // 4.得到修改结果
        System.out.println(response);
        // 5.关闭连接
        client.close();
    }

    /**
    * @Title: testDel
    * @Description: 根据ID删除
    * @Author: Alcatraz
    * @Version: 1.0
    * @Date:  2022/7/24 20:42
    * @Parameters: []
    * @Return void
    */
    @Test
    public void testDel() throws Exception{
        // 1.获取ES连接
        TransportClient client = ESClientUtil.getClient();
        // 3.使用链式编程，删除指定文档
        DeleteResponse response = client.prepareDelete("pethome", "user", "1").get();
        // 4.得到删除结果
        System.out.println(response);
        // 5.关闭连接
        client.close();
    }


     /**
     * @Title: testBatch
     * @Description: 复杂查询批量新增
     * @Author: Alcatraz
     * @Version: 1.0
     * @Date:  2022/7/24 20:42
     * @Parameters: []
     * @Return void
     */
    @Test
    public void testBatch() throws Exception {
        // 1.获取ES连接
        TransportClient client = ESClientUtil.getClient();
        // 2.创建批量操作对象
        BulkRequestBuilder bulk = client.prepareBulk();
        // 3.批量创建文档，放入批量操作对象中
        for (int i = 2; i < 100; i++) {
            IndexRequestBuilder builder = client.prepareIndex("pethome", "user", i + "").setSource(
                    XContentFactory.jsonBuilder()
                            .startObject()
                            .field("id", i - 1)
                            .field("name", "吃了吃了,好得很!" + i)
                            .field("age", i - 1)
                            .field("sex", i%2)
                            .endObject()
            );
            BulkResponse result = bulk.add(builder).get();
            System.out.println(result.status());
        }
        // 4.关闭连接
        client.close();
    }

}